﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;

namespace WebStudio.Framework.Helper
{
    public class ResourceHelper
    {
        /// <summary>
        /// Localization Enumeration Item
        /// </summary>
        /// <param name="enumerator">Target enum object</param>
        /// <param name="baseName">Resource base name</param>
        /// <returns></returns>
        public static string LocalizeEnumeration(object enumerator)
        {
            //TODO: baseName configuration
            string baseName = "";
            ResourceManager resources = new ResourceManager(baseName,
                        System.Reflection.Assembly.GetExecutingAssembly());

            Enum e = enumerator as Enum;
            string rk = String.Format("{0}.{1}.Text", enumerator.GetType().Name, enumerator);
            string localizedDescription = resources.GetString(rk);

            if (localizedDescription == null)
            {
                // A localized string was not found so you can either just return
                // the enums value - most likely readable and a good fallback.
                return enumerator.ToString();

                // Or you can return the full resourceKey which will be helpful when
                // editing the resource files(e.g. MyClass+SomeEnum.Small) 
                // return resourceKey;
            }
            else
            {
                return localizedDescription;
            }
        }
    }
}
